package com.example.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.py.mymodule.submodule.entity.Commodity;
import org.py.mymodule.submodule.entity.OrdersTails;
import org.py.mymodule.submodule.entity.OrdersTailsform;
import org.py.mymodule.submodule.mapper.CommodityMapper;
import org.py.mymodule.submodule.mapper.OrdersTailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.mysql.cj.conf.PropertyKey.logger;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-13
 */
@RestController
@RequestMapping("/orders-tails")
public class OrdersTailsController {
    @Autowired
    private OrdersTailsMapper ordersTailsMapper;
    @Autowired
    private CommodityMapper commodityMapper;

    // 订单明细的添加
    @PostMapping("/add")
    public Map<String, Object> addOrderTails(@RequestBody OrdersTailsform ordersTailsform) {
        Map<String, Object> result = new HashMap<>();

        try {
            OrdersTails ordersTails = new OrdersTails();
            ordersTails.setOrderId(ordersTailsform.getOrderId());
            ordersTails.setCommodityId(ordersTailsform.getCommodityId());
            ordersTails.setCommodityQuantity(ordersTailsform.getCommodityQuantity());

            ordersTailsMapper.insert(ordersTails); // 插入订单明细数据

            result.put("status", "success");
            result.put("message", "Order tails added successfully");
            result.put("data", ordersTails);
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "Failed to add order tails: " + e.getMessage());
        }

        return result;
    }




    // 订单明细的查看
    @GetMapping("/query/{orderId}")
    public List<OrdersTails> queryOrderTails(@PathVariable("orderId") Long orderId) {
        return ordersTailsMapper.selectList(null); // 查询订单明细数据
    }



    // 查询对应订单号商品
    @GetMapping("/query-goods/{orderId}")
    public List<Commodity> queryGoodsByOrderId(@PathVariable("orderId") Long orderId) {
        try {
            QueryWrapper<OrdersTails> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", orderId);
            List<OrdersTails> ordersTails = ordersTailsMapper.selectList(queryWrapper);

            if (ordersTails.isEmpty()) {
                return Collections.emptyList();
            }

            List<Integer> commodityIds = ordersTails.stream()
                    .map(OrdersTails::getCommodityId)
                    .collect(Collectors.toList());

            List<Commodity> commodities = commodityMapper.selectBatchIds(commodityIds);
            return commodities;

        } catch (Exception e) {
            throw new RuntimeException("Error querying goods", e);
        }
    }

}
